package com.powerteam.service.crm.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.powerteam.exception.BusinessException;
import com.powerteam.mapper.crm.ContactsMapper;
import com.powerteam.model.crm.Contacts;
import com.powerteam.service.crm.ContactsService;
import com.powerteam.vo.Result;
import com.powerteam.vo.crm.QueryContactsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 联系人相关服务接口实现
 *
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2021/12/30
 */
@Slf4j
@Service
public class ContactsServiceImpl implements ContactsService {

    private static final String CLASS_NAME = "ContactsServiceImpl";

    @Autowired
    private ContactsMapper contactsMapper;

    @Override
    @Transactional(rollbackFor = {BusinessException.class}, propagation = Propagation.REQUIRED)
    public Result insert(Contacts contacts) {

        try {

            // 创建联系人信息
            contacts.setCreateDate(new Date());
            boolean result = contactsMapper.insert(contacts) > 0;
            log.info("{} insert 创建联系人信息 入参0:{} 结果0:{}", CLASS_NAME, JSON.toJSON(contacts), result);

            return new Result(result);
        } catch (Exception e) {
            log.warn("{} insert 创建联系人信息异常 入参0:{}", CLASS_NAME, JSON.toJSON(contacts));
            log.error(e.getMessage(), e);
            throw new BusinessException("创建联系人信息错误");
        }

    }

    @Override
    public PageInfo<Contacts> find(QueryContactsVO vo) {

        try {
            // 如果没有禁用分页 设置分页信息
            if (StringUtils.isEmpty(vo.getDisablePaging()) || !vo.getDisablePaging()) {
                PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
            }
            vo.setWord(StringUtils.isEmpty(vo.getWord()) ? null : "%".concat(vo.getWord()).concat("%"));
            List<Contacts> contactsList = contactsMapper.find(vo);
            log.info("{} find 分页查询联系人信息 入参0:{} 结果0:{}", CLASS_NAME, JSON.toJSON(vo), contactsList);

            return new PageInfo<>(contactsList);
        } catch (Exception e) {
            log.warn("{} find 分页查询联系人信息异常 入参0:{}", CLASS_NAME, JSON.toJSON(vo));
            log.error(e.getMessage(), e);
            return new PageInfo<>(new ArrayList<>());
        }
    }

    @Override
    public Contacts findById(Integer contactsId) {

        try {
            Contacts contacts = contactsMapper.findById(contactsId);
            log.info("{} findById 根据Id查找联系人信息 入参0:{} 结果0:{}", CLASS_NAME, contactsId, JSON.toJSON(contacts));

            return contacts;
        } catch (Exception e) {
            log.warn("{} findById 根据Id查找联系人信息异常 入参0:{}", CLASS_NAME, contactsId);
            log.error(e.getMessage(), e);
            return new Contacts();
        }
    }

    @Override
    @Transactional(rollbackFor = {BusinessException.class}, propagation = Propagation.REQUIRED)
    public Result update(Contacts contacts) {

        try {

            // 修改联系人信息
            boolean result = contactsMapper.update(contacts) > 0;
            log.info("{} update 修改联系人信息 入参0:{} 结果0:{}", CLASS_NAME, JSON.toJSON(contacts), result);

            return new Result(result);
        } catch (Exception e) {
            log.warn("{} update 修改联系人信息异常 入参0:{}", CLASS_NAME, JSON.toJSON(contacts));
            log.error(e.getMessage(), e);
            throw new BusinessException("修改联系人信息错误");
        }
    }

    @Override
    @Transactional(rollbackFor = {BusinessException.class}, propagation = Propagation.REQUIRED)
    public Result remove(Integer contactsId) {

        try {

            // 删除联系人信息
            boolean result = contactsMapper.remove(contactsId) > 0;
            log.info("{} update 删除联系人信息 入参0:{} 结果0:{}", CLASS_NAME, contactsId, result);

            return new Result(result);
        } catch (Exception e) {
            log.warn("{} update 删除联系人信息异常 入参0:{}", CLASS_NAME, contactsId);
            log.error(e.getMessage(), e);
            throw new BusinessException("删除联系人信息错误");
        }
    }

}
